package com.xjl.javacore.code.ioexample.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author xiaojinlu1990@163.com
 * @date 2020/1/20 19:18
 */
public class udpService {
    public static void main(String[] args) throws  Exception {
        //创建
        DatagramSocket datagramSocket = new DatagramSocket(12321);
        byte[] bts = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bts,bts.length);
        //接受
        datagramSocket.receive(datagramPacket);
        String hostAddress = datagramPacket.getAddress().getHostAddress();
        int port = datagramPacket.getPort();
        byte[] data = datagramPacket.getData();
        //关闭
        datagramSocket.close();
    }

}
